/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package entities;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;
import org.apache.log4j.Logger;

/**
 *
 * @author bsmith
 */
@Entity
@Table(name = "merchants")
@XmlRootElement
@NamedQueries({
    @NamedQuery(name = "Merchants.findAll", query = "SELECT m FROM Merchants m"),
    @NamedQuery(name = "Merchants.findById", query = "SELECT m FROM Merchants m WHERE m.id = :id"),
    @NamedQuery(name = "Merchants.findByMerchant", query = "SELECT m FROM Merchants m WHERE m.merchant = :merchant"),
    @NamedQuery(name = "Merchants.findByStore", query = "SELECT m FROM Merchants m WHERE m.store = :store"),
    @NamedQuery(name = "Merchants.findByMatcher", query = "SELECT m FROM Merchants m WHERE m.matcher = :matcher")})
public class Merchants implements Serializable {

    //@Transient
    //Logger logger = Logger.getLogger(Merchants.class);
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "id")
    private Integer id;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 45)
    @Column(name = "merchant")
    private String merchant;
    @Size(max = 45)
    @Column(name = "store")
    private String store;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 128)
    @Column(name = "matcher")
    private String matcher;
    @OneToMany(mappedBy = "merchant")
    private Collection<Statement> statementCollection;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "merchantid")
    private Collection<Merchantassociatedcategories> merchantassociatedcategoriesCollection;

    public Merchants() {
    }

    public Merchants(Integer id) {
        this.id = id;
    }

    public Merchants(String merchant) {
        this(merchant, null, merchant);
    }

    public Merchants(String merchant, String store, String matcher) {
        setMerchant(merchant);
        setMatcher(matcher);
        setStore(store);
    }

    public Merchants(Integer id, String merchant, String store, String matcher) {
        this(merchant, store, matcher);
        this.id = id;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getMerchant() {
        return merchant;
    }

    public void setMerchant(String merchant) {
        this.merchant = replaceProblemChars(merchant);
    }

    public String getStore() {
        return store;
    }

    public void setStore(String store) {
        this.store = store;
    }

    public String getMatcher() {
        return matcher;
    }

    public void setMatcher(String matcher) {
        // want to get rid of characters in string that cause problems with regex matching

        this.matcher = replaceProblemChars(matcher);
    }

    public static String replaceProblemChars(String input) {
        String out = input.replaceAll("[\\[\\]\\(\\)\\{\\}]", "");
        System.out.println("Merchants - setMatcher - in:" + input + ", out:" + out);
        return out;
    }

    @XmlTransient
    public Collection<Statement> getStatementCollection() {
        return statementCollection;
    }

    public void setStatementCollection(Collection<Statement> statementCollection) {
        this.statementCollection = statementCollection;
    }

    @XmlTransient
    public Collection<Merchantassociatedcategories> getMerchantassociatedcategoriesCollection() {
        return merchantassociatedcategoriesCollection;
    }

    public void setMerchantassociatedcategoriesCollection(Collection<Merchantassociatedcategories> merchantassociatedcategoriesCollection) {
        this.merchantassociatedcategoriesCollection = merchantassociatedcategoriesCollection;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Merchants)) {
            return false;
        }
        Merchants other = (Merchants) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder().append("entities.Merchants[ id=");
        sb.append(id);
        sb.append(",  merchant=");
        sb.append(getMerchant());
        sb.append(", store=");
        sb.append(getStore());
        sb.append(", matcher=");
        sb.append(getMatcher());
        sb.append("]");
        if (getMerchantassociatedcategoriesCollection() != null) {
            for (Merchantassociatedcategories merchantassociatedcategories : getMerchantassociatedcategoriesCollection()) {
                sb.append(", cat=").append(merchantassociatedcategories.getCategoryid());
            }
        }
        return sb.toString();
    }

    /**
     * 
     * @return string representation of what to display in UI components
     */
    public String getDisplayString() {
        StringBuilder sb = new StringBuilder();
        sb.append(getMerchant());
        return sb.toString();
    }

    /* ******* */
    //@Transient
    public String getMerchantUnit() {
        StringBuilder sb = new StringBuilder();

        sb.append(getMerchant());
        if (getStore() != null && getStore().length() > 0) {
            sb.append(" x (").append(getStore()).append(")");
        }
        //System.out.println("getMerchantUnit:" + sb.toString());
        return sb.toString();
    }

    public Collection<Categories> getCategories() {
        List<Categories> categories = new ArrayList<Categories>();
        Iterator<Merchantassociatedcategories> macIterator = merchantassociatedcategoriesCollection.iterator();
        //logger.debug("Merchant categories");
        //logger.debug(macIterator);

        while (macIterator.hasNext()) {
            Categories c = macIterator.next().getCategoryid();
            //System.out.println("Merchant category:" + c + " (is " + c.getCategory() + ")");
            categories.add(c);
        }

        return categories;
    }
}
